//TODO: License Header.

/*
This code is licensed under the BSD license.

*/
package analysis.metrics

import primitives.cluster.ClusterHead
import primitives.graph.Node
import search.util.TreeLoader2
import static Math.min
import primitives.cluster.*;
import primitives.graph.*;

class WorkingMemoryLimitMetric{


//TODO: if the size of the machine < 4 what do we do?

	def static evaluate(ClusterHead tree){

		def penalties = 0
		double total = 0;

	        def modules = [];


	        tree.getChildren().each{modules.add(it)}

	        IClusterLevel current;
	        def curr = 0;
	        while (curr < modules.size()){
	            current = modules.get(curr)
	            curr++
				if(!current.knownAs(-1) || (!current.encapsulates() && numChildren(current) == 1)){
					if(current.encapsulates())
			            		modules.addAll(current.children)
			            
				    def count = numChildren(current);
				    if(count < 5 || count > 9){
					penalties++
					}
					total++
			        }
			    }
		return [penalties, total]
	}

	public static double numChildren(IClusterLevel c){
	if(!c.encapsulates()){
		return c.nodes.size();
	}else{
		return c.children.size();
	}

    }

	@Deprecated
	public static void main(String[] args){
		def filename = args[0]

		ClusterHead tree = TreeLoader2.loadTreeFromSerial(new File(filename))
		
		print evaluate(tree);
	
		
	}
	
}